package myschool.dao;

import myschool.entity.Subject;
import myschool.util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
//mysql
public class SubjectDaoImpl implements SubjectDao {
    @Override
    public int add(Subject s) {
        Connection con = DbUtil.get();
        PreparedStatement ps = null;
        String sql = "insert into s_subject(subjectname,classhour,gradeid)values(?,?,?)";
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, s.getSubjectname());
            ps.setInt(2, s.getClasshour());
            ps.setLong(3, s.getGradeid());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtil.close(ps, con);
        }
        return 0;
    }
    //根据主键改
    @Override
    public int update(Subject s) {
        String sql="update s_subject set subjectname=?,classhour=?,gradeid=? where id=?";
        return 0;
    }

    @Override
    public int del(long id) {
        return 0;
    }

    @Override
    public Subject get(long id) {
        Connection con = DbUtil.get();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from s_subject where id=?";
        try {
            ps = con.prepareStatement(sql);
            ps.setLong(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                Subject s = new Subject();
                s.setId(rs.getLong("id"));
                s.setSubjectname(rs.getString("subjectname"));
                s.setClasshour(rs.getInt("classhour"));
                s.setGradeid(rs.getLong("gradeid"));
                return s;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DbUtil.close(rs, ps, con);
        }
        return null;
    }

    //原则是，找不到，不返回null,返回空 List
    @Override
    public List<Subject> search(String sname) {
        List<Subject> datas = new ArrayList<>();
        Connection con = DbUtil.get();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from s_subject where subjectname like ?";
        try {
            ps = con.prepareStatement(sql);
            //subjectname like 'java'
            ps.setString(1,"%"+sname+"%");
            rs = ps.executeQuery();
            while (rs.next()) {
                Subject s = new Subject();
                s.setId(rs.getLong("id"));
                s.setSubjectname(rs.getString("subjectname"));
                s.setClasshour(rs.getInt("classhour"));
                s.setGradeid(rs.getLong("gradeid"));
               datas.add(s);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DbUtil.close(rs, ps, con);
        }
        return datas;
    }
}
